<?php

/*
 * Copyright 2008 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#namespace tehframework\orm\annotations;

#use \BadMethodCallException;
#use \Reflector;
#use \ReflectionProperty;
#use \ReflectionMethod;
#use \ezcPersistentObjectDefinition;
#use \ezcPersistentObjectProperty;
#use tehframework\Inflector;
#use tehframework\annotations\Annotation;
#use tehframework\annotations\AnnotationTargetAware;

class PropertyAnnotation implements Annotation, AnnotationTargetAware
{
	/**
	 * @var ReflectionProperty|ReflectionMethod
	 */
	protected $target;
	
	/**
	 * @var string?
	 */
	protected $field;
	
	/**
	 * @var PropertyTypeAnnotation
	 */
	protected $type;
	
	/**
	 * @param  string? $field
	 */
	public function __construct($field = null)
	{
		$this->setField($field);
	}
	
	/**
	 * @param  string $field
	 * @return PropertyAnnotation
	 */
	public function setField($field)
	{
		if (!empty($field))
		{
			$this->field = $field;
		}
		
		return $this;
	}
	
	/**
	 * @param  PropertyTypeAnnotation $type
	 * @return PropertyAnnotation
	 */
	public function setType(PropertyTypeAnnotation $type)
	{
		$this->type = $type;
		
		return $this;
	}
	
	/**
	 * @param  ezcPersistentObjectDefinition $def
	 */
	public function applyDefinition(ezcPersistentObjectDefinition $object)
	{
		$property = new ezcPersistentObjectProperty(
			$this->createColumnName(), $this->createPropertyName()
		);
		
		$this->applyTypeInfo($property);
		
		$object->properties[$property->propertyName] = $property;
	}
	
	/**
	 * @return string
	 */
	protected function createColumnName()
	{
		return empty($this->field)
			? Inflector::underscore($this->createPropertyName()) : $this->field;
	}
	
	/**
	 * @return string
	 */
	protected function createPropertyName()
	{
		return substr($this->target->name, 0, 3) === 'set'
			? lcfirst(substr($this->target->name, 3)) : $this->target->name;
	}
	
	/**
	 * @param  ezcPersistentObjectProperty $property
	 */
	protected function applyTypeInfo(ezcPersistentObjectProperty $property)
	{
		if ($this->type === null)
		{
			return;
		}
		
		$property->propertyType = $this->type->getPropertyType();
		$property->databaseType = $this->type->getDatabaseType();
		$property->converter    = $this->type->createConverter();
	}
	
	#region AnnotationTargetAware
	
	/**
	 * @param  Reflector $annotationTarget
	 * @throws BadMethodCallException
	 */
	public function setAnnotationTarget(Reflector $annotationTarget)
	{
		if (!($annotationTarget instanceof ReflectionProperty)
		&& !($annotationTarget instanceof ReflectionMethod))
		{
			throw new BadMethodCallException();
		}
		
		$this->target = $annotationTarget;
	}
	
	#endregion
	
	#region Annotation
	
	/**
	 * Returns a bitwise flag of targets which can be annotated
	 * with this annotation.
	 * 
	 * @return int
	 *         A bitwise flag of property and method targets. 
	 */
	public function getAnnotationTargets()
	{
		return Annotation::TARGET_PROPERTY | Annotation::TARGET_METHOD;
	}
	
	/**
	 * Determines whether annotation can be used multiple times
	 * on a single target.
	 * 
	 * @return bool
	 *         Always FALSE. This annotation can not be used multiple times on
	 *         single target.
	 */
	public function allowsMultiple()
	{
		return false;
	}
	
	#endregion
}